package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: bowen
 * @Date: 2019/7/4 15:11
 * @Description:
 */
@Service(interfaceClass = ReportService.class)
public class ReportServiceImpl implements ReportService {


	@Autowired
	MemberDao memberDao;


	@Autowired
	OrderDao orderDao;

	/**
	 *  @描述：获取运营数据
	 */
	public Map<String, Object> getBusinessReportData() {

		//reportData:{
		//	reportDate:null,
		//			todayNewMember :0,
		//			totalMember :0,
		//			thisWeekNewMember :0,
		//			thisMonthNewMember :0,    select count(id) from t_member where regTime >=  >>???
		//			todayOrderNumber :0,
		//			todayVisitsNumber :0,
		//			thisWeekOrderNumber :0,
		//			thisWeekVisitsNumber :0,
		//			thisMonthOrderNumber :0,
		//			thisMonthVisitsNumber :0,
		//			hotSetmeal :[
		//	{name:'阳光爸妈升级肿瘤12项筛查（男女单人）体检套餐',setmeal_count:200,proportion:0.222},
		//	{name:'阳光爸妈升级肿瘤12项筛查体检套餐',setmeal_count:200,proportion:0.222}
        //            ]
		//}
		Map<String,Object> map = new HashMap();
		LocalDate now = LocalDate.now();
		String today = getdateStr(now);
		// 本周一的日期字符串
		String monday = getdateStr(now.with(DayOfWeek.MONDAY));
		// 本月1号的字符串
		String first = getdateStr(now.with(TemporalAdjusters.firstDayOfMonth()));

		Integer todayNewMember = memberDao.findMemberCountByDate(today);
		Integer totalMember = memberDao.findMemberTotalCount();

		//本周新增会员数
		Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(monday);

		//本月新增会员数
		Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(first);

		//今日预约数
		Integer todayOrderNumber = orderDao.findOrderCountByDate(today);

		//本周预约数
		Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(monday);

		//本月预约数
		Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(first);

		//今日到诊数
		Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);

		//本周到诊数
		Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(monday);

		//本月到诊数
		Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(first);

		// 查询热门套餐
		List<Map> hotSetmeal = orderDao.findHotSetmeal();



		map.put("reportDate",now);
		map.put("todayNewMember",todayNewMember);
		map.put("totalMember",totalMember);
		map.put("thisWeekNewMember",thisWeekNewMember);
		map.put("thisMonthNewMember",thisMonthNewMember);
		map.put("todayOrderNumber",todayOrderNumber);
		map.put("thisWeekOrderNumber",thisWeekOrderNumber);
		map.put("thisMonthOrderNumber",thisMonthOrderNumber);
		map.put("todayVisitsNumber",todayVisitsNumber);
		map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
		map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
		map.put("hotSetmeal",hotSetmeal );
		return map;
	}

	private static String getdateStr(LocalDate now) {
		return DateTimeFormatter.ofPattern("yyyy-MM-dd").format(now.with(DayOfWeek.MONDAY));
	}

	public static void main(String[] args) {
		System.out.println(getdateStr(LocalDate.now()));
	}
}



